package com.codefish.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codefish.entity.OutMoneyClass;
import com.codefish.entity.SysUserTokenEntity;
import com.codefish.mapper.OutMoneyClassDao;
import com.codefish.service.OutMoneyClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;


@Service("outMoneyClassService")
public class OutMoneyClassServiceImpl extends ServiceImpl<OutMoneyClassDao, OutMoneyClass> implements OutMoneyClassService {

    @Autowired
    private OutMoneyClassDao classDao;

    /**
     * 新增单个
     *
     * @param outMoneyClass
     * @return
     */
    @Override
    public Integer saveOne(OutMoneyClass outMoneyClass) {
        boolean save = this.save(outMoneyClass);
        if (!save) {
            return -1;
        }
        return outMoneyClass.getId();
    }

    /**
     * 修改单个
     *
     * @param outMoneyClass
     * @return
     */
    @Override
    public boolean updateOne(OutMoneyClass outMoneyClass) {

        boolean flag = this.updateById(outMoneyClass);

        return flag;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteOne(Integer id) {

        OutMoneyClass outMoneyClass = new OutMoneyClass();
        outMoneyClass.setId(id);
        outMoneyClass.setIsDel(1);

        boolean flag = this.updateById(outMoneyClass);

        return flag;
    }

    /**
     * 条件分页查询
     *
     * @param pageNo
     * @param pageSize
     * @param familyId
     * @return
     */
    @Override
    public IPage<OutMoneyClass> getPageByParams(Integer pageNo, Integer pageSize, Integer familyId) {

        Page<OutMoneyClass> page = new Page<>(pageNo, pageSize);
        QueryWrapper<OutMoneyClass> qw = new QueryWrapper<>();
        qw.eq("state", 1).ne("is_del", 1);
        qw.eq("family_id", familyId);
        qw.orderByDesc("id");

        IPage<OutMoneyClass> retPage = baseMapper.selectPage(page, qw);

        // setNameByCode
        if (CollectionUtils.isNotEmpty(retPage.getRecords())){
            retPage.getRecords().forEach(outMoneyClass -> this.setNameByCode(outMoneyClass));
        }

        return retPage;
    }

    /**
     * 根据id查询详情
     *
     * @param id
     * @return
     */
    @Override
    public OutMoneyClass getItemById(Integer id) {

        OutMoneyClass retEntity = new OutMoneyClass();

        OutMoneyClass outMoneyClass = baseMapper.selectById(id);
        if (ObjectUtil.isNotEmpty(outMoneyClass)) {
            // setNameByCode/id
            outMoneyClass = this.setNameByCode(outMoneyClass);
            return outMoneyClass;
        }

        return retEntity;
    }

    /**
     * 根据id批量查询
     * @param classList
     * @return
     */
    @Override
    public List<OutMoneyClass> selectList(List<Integer> classList) {
        QueryWrapper<OutMoneyClass> qw = new QueryWrapper<>();
        qw.in("id",classList);

        return  baseMapper.selectList(qw);
    }

    /**
     * setNameByCode/Id
     *
     * @param outMoneyClass
     * @return
     */
    private OutMoneyClass setNameByCode(OutMoneyClass outMoneyClass) {

        if (ObjectUtil.isEmpty(outMoneyClass.getCreateId())) {
            return outMoneyClass;
        } else {
            Long createId = outMoneyClass.getCreateId();
            SysUserTokenEntity user = SysUserTokenServiceImpl.getUserById(createId);
            // set name_cn
            outMoneyClass.setNameCn(StringUtils.isNotEmpty(user.getNameCn()) ? user.getNameCn() : "");
        }

        return outMoneyClass;
    }
}